﻿//----------------------------------------------------------------
// Desc: 
// Edit: ShowLong, http://blog.csdn.net/showlong
//
// Date Created: 2009-10-13
//
// Update Date :            
//
// Copyright (c) Fuel4D (http://www.fuel4d.com), All rights reserved.
//
//----------------------------------------------------------------
#pragma once


class FUI_System;


class FAPI FUI_Drawer : public IFUI_MemObject
{
    FUI_DECLARE_CLASS(FUI_Drawer);

    friend class FUI_System;
	friend class FUI_Drawer_Register;

public:
    // 绘制一张图片到指定位置
    // .srcPic srcPic->ppic 必须是一个非0值，即在初始化UI控件的时候必须调用BindPicture接口，表示srcPic是否有效
    // .reversal 翻转模式的情况下，只支持绽放，不支持平铺、九宫等
    void Draw(fuRect* dstRect, fuPicture* srcPic);
    void Draw(fuRect* dstRect, fuPicture* srcPic, BYTE reversal);
    // 强制释放一张图片资源贴图，不考虑计数
    void Destroy(fuPicture* srcPic);
    // 释放渲染器数据，并清空
    void Release();

    // 当前渲染库的图片对象
    struct PIC_FILE
    {
        DWORD nPictureID;   // 图片ID
        DWORD nLstUsing;    // 上次使用时间
        fuSize uPicSize;    // 图片文件的源始尺寸

        PIC_FILE()
        {
            nPictureID = 0, uPicSize.nW = uPicSize.nH = 512, nLstUsing = 0;
        }
        PIC_FILE(DWORD id, funit w, funit h)
        {
            nPictureID = id, uPicSize.nW = w, uPicSize.nH = h, nLstUsing = 0;
        }
    };

	// 仓库代理接口，屏蔽STL相关接口
	class FUI_Drawer_Register_interface : public IFUI_MemObject
	{
	public:
		virtual FUI_Drawer::PIC_FILE* Find(FUI_System* mgr, fuPicture* srcPic) = 0;
		virtual void Release(FUI_System* mgr) = 0;
		virtual void Erase(FUI_System* mgr, FIDHASH res_hash) = 0;
	};
    // 查询一个已经被正常处理的图片（仅供访问，不要修改内容）
    PIC_FILE* FindPic(fuPicture* pp);

protected:
	FUI_Drawer_Register_interface* picRegisterLib;
    FUI_System* pUIMgr;             // UI管理器地址
    DWORD dwColor;                  // 当前渲染器颜色

protected:
    // 构造析构
    FUI_Drawer();
    ~FUI_Drawer();
    // 设置渲染器当前透明度，使用结束时需要将颜色恢复为1
    void SetAlpha(float alpha);
    // 渲染器组
    FINLINE void DrawExtend(PIC_FILE* picData, fuRect* dstRect, fuRect* srcRect, fuPicture* srcPicture, BYTE reversal = 0);
    FINLINE void DrawNormal(PIC_FILE* picData, fuRect* dstRect, fuRect* srcRect, fuPicture* srcPicture);
    FINLINE void DrawTiled(PIC_FILE* picData, fuRect* dstRect, fuRect* srcRect, fuPicture* srcPicture, BYTE single_mode = 0);
};
